package cbbx_sm.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class FormatResultsForStata {

	public static final String outputFileName =
		"output/schedulerEvaluationResults.txt";
	
	public static final String stateFileName =
		"output/stata.csv";
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		ArrayList<String> lines = new ArrayList<String>();

		for (String line: FileUtils.readLines(new File(outputFileName))) {
			lines.add(line);
		}
		
		// Find the last title line.
		int titleLine = lines.size() - 1;
		for (; titleLine >= 0; titleLine--) {
			if (lines.get(titleLine).contains("Date")) {
				break;
			}
		}

		// Go over the lines and extract the results.
		List<String> linesToPrint = new ArrayList<String>();
		for (int i = titleLine + 1; i < lines.size(); i++) {	
			String[] values =  lines.get(i).split("\t");
			String[] lookaheadParams = values[5].split("_");
			
			if (values[5].contains("LookAheadDecisionMaker") 
					|| values[5].contains("oracle")
					|| values[5].contains("SmartMostLikely")) {
				StringBuilder lookaheadParamsString = new StringBuilder();
				for (int j=0; j < lookaheadParams.length; j++) {
					if (j > 0) {
						lookaheadParamsString.append("\t");
					}
					lookaheadParamsString.append(lookaheadParams[j]);
				}
				linesToPrint.add(String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s%n",
						values[0], values[3],values[4],values[2],lookaheadParamsString.toString(),
						values[6],values[7],values[8],values[9],values[10]));
			} else {

				// Add duplicate entries for the baseline alternatives, based on the best values.
				for (double beta = 0d; beta <= 1d; beta += 0.1d) {
					StringBuilder lookaheadParamsString = new StringBuilder();
					lookaheadParamsString.append(values[5]);
					// Add tabs for the non lookahead techniques
					lookaheadParamsString.append(
							String.format("\t\t%f\t%f\t", (double)1, beta));
					for (int j=0; j< 2; j++) {
						lookaheadParamsString.append("\t");
					}
					linesToPrint.add(String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s%n",
							values[0],values[3],values[4],values[2],lookaheadParamsString.toString(),
							values[6],values[7],values[8],values[9],values[10]));	
				}
			}
		}
		//Print title
		StringBuilder output = new StringBuilder();
		output.append(String.format(
				"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s%n",
				"Timestamp",
				"Probabilistic Model",
				"Time Window",
				"Number of Regions",
				"Prediction Technique",
				"Number of Cameras",
				"Alpha",
				"Beta",
				"Precision",
				"State Size Limit",
				"Lookahead Time",
				"High Resolution Count",
				"Low Resolution Count",
				"Number of Zooms with Misses",
				"Zoom miss Rate",
				"Latency"
				));

		//Print the lines
		for (String s : linesToPrint) {
			output.append(s);
		}
		System.out.print(output.toString());
		FileUtils.writeStringToFile(new File(stateFileName), output.toString());
	}
}
